import random

import requests

from bs4 import BeautifulSoup


url = 'https://www.taopiaopiao.com/showList.htm'
# 伪装模拟 headers 请求头<字典数据类型>
user_agents = [
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0;.NET CLR 2.0.50727; SLCC2;.NET CLR 3.5.30729;.NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3;.NET4.0C;.NET4.0E)",
    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729;.NET CLR 3.0.30729; Media Center PC 6.0;.NET4.0C;.NET4.0E)",
    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1;) AppleWebKit/534.12 (KHTML, like Gecko) Maxthon/3.0 Safari/534.12",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729;.NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3;.NET4.0C;.NET4.0E)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729;.NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3;.NET4.0C;.NET4.0E; SE 2.X MetaSr 1.0)",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.33 Safari/534.3 SE 2.X MetaSr 1.0",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729;.NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3;.NET4.0C;.NET4.0E)"
]

random_user_agent = random.choice(user_agents)

headers = {
    "User-Agent": random_user_agent
}

# 发送GET请求到URL
# response = requests.get(url)
response = requests.get(url=url, headers=headers)
html_content = '''...你的HTML代码...'''

soup = BeautifulSoup(response.text, 'html.parser')

# 找到正在热映的电影列表
movie_cards = soup.select('.tab-movie-list .movie-card-wrap')

hot_movies = []

for card in movie_cards:
    # 提取电影名称
    movie_name = card.select_one('.movie-card-name .bt-l').text.strip()

    # 提取导演信息
    director_info = card.select_one('.movie-card-info span:-soup-contains("导演：")')
    director = director_info.text.split('：')[-1].strip() if director_info else ''

    # 提取主演信息
    actor_info = card.select_one('.movie-card-info span:-soup-contains("主演：")')
    actors = actor_info.text.split('：')[-1].strip() if actor_info else ''

    # 提取类型信息
    genre_info = card.select_one('.movie-card-info span:-soup-contains("类型：")')
    genres = genre_info.text.split('：')[-1].strip() if genre_info else ''

    # 提取地区信息
    region_info = card.select_one('.movie-card-info span:-soup-contains("地区：")')
    region = region_info.text.split('：')[-1].strip() if region_info else ''

    # 提取语言信息
    language_info = card.select_one('.movie-card-info span:-soup-contains("语言：")')
    language = language_info.text.split('：')[-1].strip() if language_info else ''

    # 提取片长信息
    duration_info = card.select_one('.movie-card-info span:-soup-contains("片长：")')
    duration = duration_info.text.split('：')[-1].strip() if duration_info else ''

    hot_movies.append({
        'movie_name': movie_name,
        'director': director,
        'actors': actors,
        'genres': genres,
        'region': region,
        'language': language,
        'duration': duration
    })

# 打印热映电影信息列表
for movie in hot_movies:
    print(movie)

